//
//  GHHomeControlTableViewCell.m
//  GodHorses
//
//  Created by Mac on 2017/11/17.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHHomeControlTableViewCell.h"

@implementation GHHomeControlTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = GH_backgroundColor;
    }
    return self;
}
- (void)setArray:(NSArray *)array{
    _array = array;
    for (UIView * view in self.subviews) {
        [view removeFromSuperview];
    }
    //开始布局
    for (int i = 0; i < array.count; i++) {
        GHGoodModel * model = array[i];
        UIControl * control = [self controlWithImageUrl:model.g_img tag:i title:model.g_name des:model.c_name];
        control.left = i%4 * (UIScreenWidth / 4);
        control.top = i/4 * home_cell_controHeight;
        [self addSubview:control];
    }
}
- (UIControl*)controlWithImageUrl:(NSString*)imageUrl tag:(NSInteger)tag title:(NSString*)title des:(NSString*)desc{
    CGFloat controlWitdh =  UIScreenWidth / 4;
    UIControl * control = [[UIControl alloc] initWithFrame:CGRectMake(0, 0,controlWitdh, home_cell_controHeight)];
    control.backgroundColor = GH_backgroundColor;
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, controlWitdh - 10, control.st_height - 10)];
    whiteView.layer.cornerRadius = 5;
    whiteView.clipsToBounds = YES;
    whiteView.backgroundColor = [UIColor whiteColor];
    [whiteView st_showBottomShadow];
    whiteView.userInteractionEnabled = NO;
    [control addSubview:whiteView];
    
    //whiteView 上 放图片
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(3, 3, whiteView.width - 6 , 90)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] completed:nil];
    imageView.userInteractionEnabled = NO;
    [whiteView addSubview:imageView];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(3, imageView.st_bottom, whiteView.width - 6, 14)
                                                     text:title
                                                textColor:GH_firstTextColor
                                                     font:13
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [whiteView addSubview:titleLable];
    
    STLabel * descLable = [[STLabel alloc] initWithFrame:CGRectMake(3, titleLable.st_bottom  + 3, whiteView.width - 6, 14)
                                                     text:desc
                                                textColor:GH_YellowBackGroundColor
                                                     font:13
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [whiteView addSubview:descLable];
    [control addTarget:self action:@selector(onSelectedControl:) forControlEvents:UIControlEventTouchUpInside];
    return control;
    
}
- (void)onSelectedControl:(UIControl*)sender{
    GHGoodModel * model = self.array[sender.tag];
    if (self.buttonAction) {
        self.buttonAction(model);
    }
}
@end
